import datetime
import re

import requests
from bs4 import BeautifulSoup

BASE_URL = 'https://ncov.dxy.cn/ncovh5/view/pneumonia'
HEADER = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/103.0.0.0 Safari/537.36'
}


response = requests.get(url=BASE_URL, headers=HEADER)
response.encoding = 'UTF-8'
html = response.text

# getAreaStat
soupObject = BeautifulSoup(html, 'lxml')
rawData = soupObject.select_one('#getAreaStat').text
JSON_DATA = re.findall(r'\[.+\]', rawData)[0]
nowTime = datetime.datetime.now()
nowTime = datetime.datetime.strftime(nowTime, '%Y年%m月%d日%H时%M分%S秒')

with open(f'全国疫情数据-{nowTime}.json', 'w', encoding='UTF-8') as fp:
    fp.write(JSON_DATA)
